package com.ybxx.base.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ybxx.base.model.Community;
import com.ybxx.base.model.Room;
import com.ybxx.base.model.RoomInfo;
import com.ybxx.base.service.CommunityService;
import com.ybxx.base.service.RoomInfoService;
import com.ybxx.base.service.RoomService;
import com.ybxx.config.model.HouseType;
import com.ybxx.config.service.HouseTypeService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.StringUtil;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class RoomInfoAction extends BaseAction {
	@Resource
	private RoomService roomService;
	@Resource
	private RoomInfoService roomInfoService;
	@Resource
	private CommunityService communityService;
	@Resource
	private HouseTypeService houseTypeService;
	private String id;
	private String cid;
	private String bid;
	private String uid;
	private String rid;
	private JSONObject resultObj;
	private List rooms;
	private RoomInfo ri;
	private Room room;
	private List<HouseType> houseTypes;
	private String msg="succ";
	private String urlSign;
	private String ymonth;
	/*加载需要浏览的房间信息*/
	public String view(){
		Object[] obj=this.roomInfoService.load(rid);
		ri=(RoomInfo)obj[0];
		room=(Room)obj[1];
		return "view";
	}
	/*删除房屋信息*/
	public String remove(){
		try{
			roomService.remove(rid);
		}catch(Exception ex){
			msg="房屋信息删除失败";
		}finally{
			return "succ";
		}
	}
	/*保存房屋信息*/
	public String save(){
		try{
			if(StringUtils.isEmpty(ri.getIcode())){
				ri.setIcode(UUIDGenerator.getUUID());
			}
			roomInfoService.save(ri);
		}catch(Exception ex){
			msg="房间维护失败";
		}finally{
			return "succ";
		}
	}
	/*加载需要修改的房间信息*/
	public String load(){
		Object[] obj=this.roomInfoService.load(rid);
		ri=(RoomInfo)obj[0];
		room=(Room)obj[1];
		houseTypes=houseTypeService.getAll();
		return "load";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		/**获取当前登录者*/
		User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
		rooms=roomInfoService.queryPage(filter,cid,bid,uid,rid,user.getLoginid());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/**房间管理，查询套户资料*/
	public String query(){
		/**获取当前登录者*/
		User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
		List<Community> communitys=communityService.queryTree(user.getLoginid());
		JSONArray arrayOne =new JSONArray();
		for(Iterator<Community> iter=communitys.iterator();iter.hasNext();){
			Community community=iter.next();
			JSONObject jsonObj=new JSONObject();
			jsonObj.put("id", "C"+community.getCid());
			jsonObj.put("name",community.getCname());
			jsonObj.put("pId","0");
			if(urlSign!=null && ("printcheck".equals(urlSign) || "fees".equals(urlSign))){
				jsonObj.put("isParent",true);
				jsonObj.put("nocheck",false);
			}else{
				jsonObj.put("isParent",true);
				jsonObj.put("nocheck",true);
			}
			
			if(urlSign==null || "".equals(urlSign)){
				jsonObj.put("url","../base/queryPage_RoomInfo.action?cid="+community.getCid()+"&urlSign="+urlSign);
				jsonObj.put("target","_self");
			}else if("residents".equals(urlSign)){
				jsonObj.put("url","../base/queryPage_Residents.action?cid="+community.getCid()+"&urlSign="+urlSign);
				jsonObj.put("target","_self");
			}else if("anmeterManager".equals(urlSign)){
				jsonObj.put("url","../dashboard/init_Anmeter.action?cid="+community.getCid()+"&urlSign="+urlSign);
				jsonObj.put("target","_self");
			}else if("watermeterManager".equals(urlSign)){
				jsonObj.put("url","../dashboard/init_Watermeter.action?cid="+community.getCid()+"&urlSign="+urlSign);
				jsonObj.put("target","_self");
			}else if("anmeterread".equals(urlSign)){
				jsonObj.put("url","");//"../dashboard/queryReadForm_Anmeter.action?unitid=C"+community.getCid()+"&urlSign="+urlSign+"&ymonth="+ymonth);
				jsonObj.put("target","_self");
				jsonObj.put("click","showAnmeterRead('C"+community.getCid()+"','"+urlSign+"');");
			}else if("fees".equals(urlSign)){
				jsonObj.put("url","");//"../dashboard/queryReadForm_Anmeter.action?unitid=R"+room.getRid()+"&urlSign="+urlSign+"&ymonth="+ymonth);
				jsonObj.put("target","_self");
				jsonObj.put("click","showFees('C"+community.getCid()+"','"+urlSign+"');");
			}
			
			arrayOne.add(jsonObj);
		}
		resultObj=new JSONObject();
		resultObj.put("communitys", arrayOne);
		return "query";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public JSONObject getResultObj() {
		return resultObj;
	}
	public void setResultObj(JSONObject resultObj) {
		this.resultObj = resultObj;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getBid() {
		return bid;
	}
	public void setBid(String bid) {
		this.bid = bid;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getRid() {
		return rid;
	}
	public void setRid(String rid) {
		this.rid = rid;
	}
	public List getRooms() {
		return rooms;
	}
	public void setRooms(List rooms) {
		this.rooms = rooms;
	}
	public RoomInfo getRi() {
		return ri;
	}
	public void setRi(RoomInfo ri) {
		this.ri = ri;
	}
	public Room getRoom() {
		return room;
	}
	public void setRoom(Room room) {
		this.room = room;
	}
	public List<HouseType> getHouseTypes() {
		return houseTypes;
	}
	public void setHouseTypes(List<HouseType> houseTypes) {
		this.houseTypes = houseTypes;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getUrlSign() {
		return urlSign;
	}
	public void setUrlSign(String urlSign) {
		this.urlSign = urlSign;
	}
	public String getYmonth() {
		return ymonth;
	}
	public void setYmonth(String ymonth) {
		this.ymonth = ymonth;
	}
	
	
}
